package com.itany.netclass.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mvc.annotation.RequestMapping;
import com.itany.mvc.annotation.ResponseBody;
import com.itany.netclass.constant.CourseTypeConstant;
import com.itany.netclass.constant.PageConstant;
import com.itany.netclass.constant.ResponseCodeConstant;
import com.itany.netclass.entity.CourseType;
import com.itany.netclass.entity.User;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.CourseTypeService;
import com.itany.netclass.util.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:21-10-14 上午8:53
 * Version:1.0-SNAPSHOT
 * Description:
 */
@RequestMapping("/courseType")
public class CourseTypeController {
    @RequestMapping("/findAllCourseType")
    public String findAllCourseType(HttpServletRequest request, HttpServletResponse response){
        CourseTypeService courseTypeService = (CourseTypeService) ObjectFactory.getObject("CourseTypeService");
        User user = (User) request.getSession().getAttribute("loginUser");
        List<CourseType> list=null;
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        String parentId = request.getParameter("parentId");
        if(parentId==null){
            request.getSession().setAttribute("parentId", CourseTypeConstant.COURSETYPE_FATHER);
        }else {
            request.getSession().setAttribute("parentId",parentId);
        }
        System.out.println(request.getSession().getAttribute("parentId"));
        if(pageNo==null){
            pageNo= PageConstant.PAGE_KAISHI_NO;
        }
        if(pageSize==null){
            pageSize=PageConstant.PAGE_SIZE;
        }
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            list=courseTypeService.findAllCourseType(user,parentId);
            PageInfo<CourseType> pageInfo = new PageInfo<>(list);
            request.getSession().setAttribute("courseTypeList",pageInfo);
            return "/showCourseType.do";
        } catch (NoLoginException e) {
            e.printStackTrace();
            return "redirect:/showLogin.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return "redirect:/showLogin.do";
        }
    }
    @ResponseBody
    @RequestMapping("/findCourseTypeById")
    public ResponseResult findCourseTypeById(HttpServletRequest request, HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String id = request.getParameter("id");
        CourseTypeService courseTypeService = (CourseTypeService) ObjectFactory.getObject("CourseTypeService");
        try {
            CourseType courseType=courseTypeService.findCourseTypeById(id);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(courseType);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/addCourseType")
    public ResponseResult addCourseType(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        CourseTypeService courseTypeService = (CourseTypeService) ObjectFactory.getObject("CourseTypeService");
        String typeName = request.getParameter("typeName");
        String parentId = request.getParameter("parentId");
        try {
            courseTypeService.addCourseType(typeName,parentId);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (TypeExistException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    @RequestMapping("/backCourseType")
    public String backCourseType(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        System.out.println(id);
        CourseTypeService courseTypeService = (CourseTypeService) ObjectFactory.getObject("CourseTypeService");
        String pageNo = request.getParameter("pageNo");
        System.out.println("pageNo="+pageNo);
        if(pageNo==null){
            pageNo=PageConstant.PAGE_KAISHI_NO;
        }
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(PageConstant.PAGE_SIZE));
            List<CourseType> list=courseTypeService.backCourseType(id);
            PageInfo<CourseType> pageInfo = new PageInfo<>(list);
            request.getSession().setAttribute("parentId",list.get(0).getParentId());
            System.out.println(request.getSession().getAttribute("parentId"));
            request.getSession().setAttribute("courseTypeList",pageInfo);
            return "/showCourseType.do";
        } catch (NoFatherException e) {
            e.printStackTrace();
            return "redirect:/courseType/findAllCourseType.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/showCourseType.do";
        }
    }
    @ResponseBody
    @RequestMapping("/modifyCourseTypeById")
    public  ResponseResult modifyCourseTypeById(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String id = request.getParameter("id");
        System.out.println("id="+id);
        String typeName = request.getParameter("typeName");
        System.out.println("typeName="+typeName);
        String parentId = request.getParameter("parentId");
        System.out.println("parentId="+parentId);
        CourseTypeService courseTypeService = (CourseTypeService) ObjectFactory.getObject("CourseTypeService");
        try {
            courseTypeService.modifyCourseTypeById(id,typeName,parentId);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (TypeExistException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/yongCourseTypeById")
    public  ResponseResult yongCourseTypeById(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String id = request.getParameter("id");
        System.out.println("id="+id);
        String status = request.getParameter("status");
        System.out.println("status="+status);
        CourseTypeService courseTypeService= (CourseTypeService) ObjectFactory.getObject("CourseTypeService");
        try {
            courseTypeService.yongCourseTypeById(id,status);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (FatherJinyongException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }


}
